﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.DBUtility;
using System.Data;
using StoragePeriod.IDAL.StorageForecast;
using System.Data.SqlClient;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.SqlServerDAL.StorageForecast
{
    /// <summary>
    /// 数据访问类:QualityResult
    /// </summary>
    public partial class D_QualityResult : I_QualityResult
    {
        public D_QualityResult()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from QualityResult");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_QualityResult model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into QualityResult(");
            strSql.Append("RawID,RawName,QuotaID,QuotaName,CalculateTypeID,CalculateResult,AddTime,NormID,ElectValue,OAreaID,TeamID)");
            strSql.Append(" values (");
            strSql.Append("@RawID,@RawName,@QuotaID,@QuotaName,@CalculateTypeID,@CalculateResult,@AddTime,@NormID,@ElectValue,@OAreaID,@TeamID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@QuotaName", SqlDbType.NVarChar,50),
					new SqlParameter("@CalculateTypeID", SqlDbType.Int,4),
					new SqlParameter("@CalculateResult", SqlDbType.NVarChar,300),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@NormID", SqlDbType.Int,4),
					new SqlParameter("@ElectValue", SqlDbType.NVarChar,50),
					new SqlParameter("@OAreaID", SqlDbType.Int,4),
					new SqlParameter("@TeamID", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.RawName;
            parameters[2].Value = model.QuotaID;
            parameters[3].Value = model.QuotaName;
            parameters[4].Value = model.CalculateTypeID;
            parameters[5].Value = model.CalculateResult;
            parameters[6].Value = model.AddTime;
            parameters[7].Value = model.NormID;
            parameters[8].Value = model.ElectValue;
            parameters[9].Value = model.OAreaID;
            parameters[10].Value = model.TeamID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_QualityResult model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update QualityResult set ");
            strSql.Append("RawID=@RawID,");
            strSql.Append("RawName=@RawName,");
            strSql.Append("QuotaID=@QuotaID,");
            strSql.Append("QuotaName=@QuotaName,");
            strSql.Append("CalculateTypeID=@CalculateTypeID,");
            strSql.Append("CalculateResult=@CalculateResult,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("NormID=@NormID,");
            strSql.Append("ElectValue=@ElectValue,");
            strSql.Append("OAreaID=@OAreaID,");
            strSql.Append("TeamID=@TeamID");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@QuotaName", SqlDbType.NVarChar,50),
					new SqlParameter("@CalculateTypeID", SqlDbType.Int,4),
					new SqlParameter("@CalculateResult", SqlDbType.NVarChar,300),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@NormID", SqlDbType.Int,4),
					new SqlParameter("@ElectValue", SqlDbType.NVarChar,50),
					new SqlParameter("@OAreaID", SqlDbType.Int,4),
					new SqlParameter("@TeamID", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.RawName;
            parameters[2].Value = model.QuotaID;
            parameters[3].Value = model.QuotaName;
            parameters[4].Value = model.CalculateTypeID;
            parameters[5].Value = model.CalculateResult;
            parameters[6].Value = model.AddTime;
            parameters[7].Value = model.NormID;
            parameters[8].Value = model.ElectValue;
            parameters[9].Value = model.OAreaID;
            parameters[10].Value = model.TeamID;
            parameters[11].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QualityResult ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QualityResult ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_QualityResult GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RawID,RawName,QuotaID,QuotaName,CalculateTypeID,CalculateResult,AddTime,NormID,ElectValue,OAreaID,TeamID from QualityResult ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_QualityResult model = new E_QualityResult();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawID"].ToString() != "")
                {
                    model.RawID = int.Parse(ds.Tables[0].Rows[0]["RawID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawName"] != null)
                {
                    model.RawName = ds.Tables[0].Rows[0]["RawName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QuotaID"].ToString() != "")
                {
                    model.QuotaID = int.Parse(ds.Tables[0].Rows[0]["QuotaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["QuotaName"] != null)
                {
                    model.QuotaName = ds.Tables[0].Rows[0]["QuotaName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CalculateTypeID"].ToString() != "")
                {
                    model.CalculateTypeID = int.Parse(ds.Tables[0].Rows[0]["CalculateTypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CalculateResult"] != null)
                {
                    model.CalculateResult = ds.Tables[0].Rows[0]["CalculateResult"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NormID"].ToString() != "")
                {
                    model.NormID = int.Parse(ds.Tables[0].Rows[0]["NormID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ElectValue"] != null)
                {
                    model.ElectValue = ds.Tables[0].Rows[0]["ElectValue"].ToString();
                }
                if (ds.Tables[0].Rows[0]["OAreaID"].ToString() != "")
                {
                    model.OAreaID = int.Parse(ds.Tables[0].Rows[0]["OAreaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TeamID"].ToString() != "")
                {
                    model.TeamID = int.Parse(ds.Tables[0].Rows[0]["TeamID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RawID,RawName,QuotaID,QuotaName,CalculateTypeID,CalculateResult,AddTime,NormID,ElectValue,OAreaID,TeamID ");
            strSql.Append(" FROM QualityResult ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RawID,RawName,QuotaID,QuotaName,CalculateTypeID,CalculateResult,AddTime,NormID,ElectValue,OAreaID,TeamID ");
            strSql.Append(" FROM QualityResult ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetPageList(E_QualityResult Model)
        {
            DataTable dt = new DataTable();
            int StartNum=(Model.PageIndex-1)*Model.PageSize;
            int EndNum=StartNum+Model.PageSize;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from(");
            strSql.Append("select ROW_NUMBER() OVER ( ORDER BY AddTime DESC ) AS RID,* from dbo.QualityResult ");
            if(Model.SearchWhere!=null&&Model.SearchWhere.Length>0)
            {
                strSql.Append(" where "+Model.SearchWhere);
            }
            strSql.Append(") as Temp where RID BeTween " + StartNum + " and " + EndNum);
            dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
            return dt;
        }

        /// <summary>
        /// 获取分页数据总量
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int GetPageCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from dbo.QualityResult where " + strWhere);
            return int.Parse(DbHelperSQL.GetSingle(strSql.ToString()).ToString());
        }
        #endregion  Method
    }
}
